// Learn TypeScript:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html

import DBMgr from "../db/DBMgr";
import Global from "../Global";
import UINode from "../ui/UINode";
import ResMgr from "./ResMgr";



//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
export default class Tool {

    public static clone(node:cc.Node, bindTo:UINode = null):cc.Node {
        let clone = cc.instantiate(node);
        if (bindTo != null) {
            let button = clone.getComponent(cc.Button);
            if (button != null) {
                this.onRegisterClick(button, bindTo);
            }
            let label = clone.getComponent(cc.Label);
            if (label != null) {
                this.onDefaultLanguage(label);
            }
        }
        this.collectChild(clone, bindTo, clone);
        return clone;
    }

    //判断label是否有直接使用language
    private static onDefaultLanguage(label:cc.Label) {
        let content = label.string;
        let res = content.match("<l>\\d*</l>");
        if (res == null) { return; }
        let languageId = Number(res[0].replace("<l>", "").replace("</l>", ""));
        let str = DBMgr.data.languageDB[languageId][Global.language];
        label.string = label.string.replace(res[0], str);
        this.onDefaultLanguage(label);
    }
    //注册按钮监听
    private static onRegisterClick(button:cc.Button, bindTo) {
        if(bindTo==null){return;}
        if(bindTo["on_click_"+button.node.name] == null) { return; }
        button.node.on("click", function(){
            bindTo.on_click_common(button);
            bindTo["on_click_"+button.node.name] && bindTo["on_click_"+button.node.name](button);
        }, this);
    }

    //遍历寻找子节点
    public static collectChild(parent:cc.Node, bindTo, clone) {
        if (parent == null || parent.children == null || parent.children.length == 0) { return; }
        for (let i = 0; i < parent.children.length; i++) {
            let child = parent.children[i];
            let name = child.name;
            if (name == "name" || name == "view") {
                name = "default_"+name;
            }
            clone[name] = child;

            let button = child.getComponent(cc.Button);
            if (button != null) {
                this.onRegisterClick(button, bindTo);
            }
            let label = child.getComponent(cc.Label);
            if (label != null) {
                this.onDefaultLanguage(label);
            }

            this.collectChild(child, bindTo, clone);
        }
    }

    //获取语言
    public static getLanguage(id, ch = Global.language) {
        return DBMgr.data.languageDB[id][ch];
    }

    //替换Language
    public static replaceLanguage(id, array) {
        let str = Tool.getLanguage(id) as string;
        for (let i = 0; i < array.length; i++) {
            str = str.replace("%s", array[i]);
        }
        return str;
    }

    //替换。。。
    public static replaceOverSize(str, size, target) {
        let isshow = true;
        let length = 0;
        let lengthShow = 0;
        for (let i = 0; i < str.length; i++) {
            if (str[i] == "⊢") {
                isshow = !isshow;
                if(isshow == true)size--;
            }
            if(isshow == false){
                size++;
            }
        }
        if (str.length < size) { return str; }
        return str.split("", size).join("") + target;
    }

    //转中文数字
    public static toChineseNum(num:number) {
        const keys = ["零","一","二","三","四","五","六","七","八","九"];
        const count = ["","十","百","千"];
        var str = "",
            nums = num.toString().split("").reverse();
    
        nums.map(function(value, index){
            str = keys[value] + 
                (Number(value) == 0 ? "" : count[ index > 3 ? index % 4 : index ]) +
                (index == 4 ? "万" : "") +
                str;
        })
    
        /*
         * 需要去掉的零：
         * 1.后面跟着零的零
         * 2.最后连续的零
         * 3.万前面的零
         */
        return str.replace(/零(?=零)|零$|零(?=万)/g,"");
    }

    //服务器时间
    public static getServerTime() {
        return (new Date().getTime())+Global.timeDiffence;
    }

    //获取消息的真实id
    public static msgRealId(msgId) {
        let realid: number = msgId%10000;
        return realid;
    }
}
